<?php
function get_sql($sql)
{
	global $db;
	$get_sql_query_statement = $db->query($sql);
	if ($result_get_sql_query = $db->fetch($get_sql_query_statement))
	{
		return $result_get_sql_query[0];
	}
	else
	{
		return "SQL_NULL";
	}
}
function	get_user($id,$value)
{
	global $db;
	
	$r	=	$db->select("user","id = '".$id."'");
	while ($row = $db->fetch($r))
		return $row[$value];
}

function	kt_user_dung($txt_username)
{
	return (!preg_match("#(^[a-z]+([a-z\_0-9\-]*))$#", $txt_username, $match));
}

function	get_bien($id)
{
	global $db;
	
	$r	=	$db->select("bien","ten = '".$id."'");
	while ($row = $db->fetch($r))
		return $row["gia_tri"];
}

function	kt_email_dung($txt_email)
{
	return (!preg_match("#[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+#", $txt_email, $match	));	
}
function	show_order($name,$sum,$pos,$width,$style=1)
{
?>
<select name="<?=$name?>" dir="rtl" size="1" class="inputbox" style="width:<?=$width?>;<?=$style==1?"font-weight:bold;color:red;":""?>">
<?php
	for ($i = 1; $i <= $sum; $i++)
	{
		echo "<option value=".$i;
		if ($pos == $i) echo " selected ";
		echo ">".$i."</option>";
	}
?>
</select>
<?php
}
// admin_load
function	admin_load($thong_bao,$url)
{
?>
<center>
	<b><font size="2"><?=$thong_bao?></font></b>
	<br /><img vspace="3" src="images/83.gif" />
	<br>Xin đợi vài giây hoặc bấm <b><a href="<?=$url?>">vào đây</a></b> để tiếp tục...
</center>
<head>
	<meta http-equiv="Refresh" content="1; URL=<?=$url?>">
	<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<?php
	die();
}
// resize hình ảnh bất kỳ
function img_resize($src,$dis,$par)
{
 	require_once('../lib/phpthumb/phpthumb.class.php');
 	$phpThumb = new phpThumb();
 	$phpThumb->src = $src;
		$r = explode("&",$par);
		for ($i = 0; $i <= count($r); $i++)
		{
			if (isset($r[$i]))
			{
				$q = explode("=",$r[$i]);
				if ($q[0] == 'h') 
					$phpThumb->h = $q[1];
				if ($q[0] == 'w') 
					$phpThumb->w = $q[1];
					
				if ($q[0] == 'zc')
				{
					$phpThumb->zc = $q[1];
				}
				
				if ($q[0] == 'fltr[]')
				{
					$phpThumb->fltr[] = $q[1];
				}
			}
		}
	$phpThumb->q = 100;
	$phpThumb->config_output_format = 'jpeg';
	$phpThumb->config_error_die_on_error = true;
	if ($phpThumb->GenerateThumbnail())
	{
		$phpThumb->RenderToFile($dis);
  	}
  	else
	{
  	}
}
// resize hình ảnh bất kỳ
function img_png_resize($src,$dis,$par)
{
 	require_once('../lib/phpthumb/phpthumb.class.php');
 	$phpThumb = new phpThumb();
 	$phpThumb->src = $src;
		$r = explode("&",$par);
		for ($i = 0; $i <= count($r); $i++)
		{
			if ($r[$i] != "")
			{
				$q = explode("=",$r[$i]);
				if ($q[0] == 'h') 
					$phpThumb->h = $q[1];
				if ($q[0] == 'w') 
					$phpThumb->w = $q[1];
					
				if ($q[0] == 'zc')
				{
					$phpThumb->zc = $q[1];
				}
				
				if ($q[0] == 'fltr[]')
				{
					$phpThumb->fltr[] = $q[1];
				}
			}
		}
	$phpThumb->q = 100;
	$phpThumb->config_output_format = 'png';
	$phpThumb->config_error_die_on_error = true;
	if ($phpThumb->GenerateThumbnail())
	{
		$phpThumb->RenderToFile($dis);
  	}
  	else
	{
  	}
}

function resize($img, $w, $h, $newfilename) {
 
 //Check if GD extension is loaded
 if (!extension_loaded('gd') && !extension_loaded('gd2')) {
  trigger_error("GD is not loaded", E_USER_WARNING);
  return false;
 }
 
 //Get Image size info
 $imgInfo = getimagesize($img);
 switch ($imgInfo[2]) {
  case 1: $im = imagecreatefromgif($img); break;
  case 2: $im = imagecreatefromjpeg($img);  break;
  case 3: $im = imagecreatefrompng($img); break;
  default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
 }
 
 //If image dimension is smaller, do not resize
 if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
  $nHeight = $imgInfo[1];
  $nWidth = $imgInfo[0];
 }else{
                //yeah, resize it, but keep it proportional
  if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
   $nWidth = $w;
   $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
  }else{
   $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
   $nHeight = $h;
  }
 }
 $nWidth = round($nWidth);
 $nHeight = round($nHeight);
 
 $newImg = imagecreatetruecolor($nWidth, $nHeight);
 
 /* Check if this image is PNG or GIF, then set if Transparent*/  
 if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
  imagealphablending($newImg, false);
  imagesavealpha($newImg,true);
  $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
 }
 imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
 
 //Generate the file, and rename it to $newfilename
 switch ($imgInfo[2]) {
  case 1: imagegif($newImg,$newfilename); break;
  case 2: imagejpeg($newImg,$newfilename);  break;
  case 3: imagepng($newImg,$newfilename); break;
  default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
 }
   
   return $newfilename;
}

function get_currency_list() {
	$db = new Database();
	$query = "SELECT id, currency_code From currency";
	$r = $db->query($query);
	return $db->fetch_array();
}

function get_location_list() {
	$db = new Database();
	$query = "SELECT id, name From location";
	$r = $db->query($query);
	return $db->fetch_array();
	
}

function add_dimension($w,$h)
{
	$dimension = array();
	$dimension['w'] = $w;
	$dimension['h'] = $h;
	return $dimension;
}

function get_all_dimensions()
{
	$dimensions = array();
	$dimensions['tour_price_'] = add_dimension(150,108);
	$dimensions['tour_ideas_'] = add_dimension(450,180);
	$dimensions['tour_ads_'] = add_dimension(160,210);
	$dimensions['loc_'] = add_dimension(118,90);
	return $dimensions;
}

function check_and_upload_picture($input_name, $display_name, $http_post_files, $prefixes = NULL) {
	
	$dimensions = get_all_dimensions();
	// this function will upload an image to server and then create an associated object for this image
	$max_file_size	=	2048000;
	$up_dir			=	"../uploads/images/";
	$up_dir_to_insert = "/uploads/images/";
	$file_type = $http_post_files[$input_name]['type'];
	$file_name = $http_post_files[$input_name]['name'];
	$file_size = $http_post_files[$input_name]['size'];
	switch ($file_type)
	{
		case "image/pjpeg"	: $file_type = "jpg"; break;
		case "image/jpeg"	: $file_type = "jpg"; break;
		case "image/gif" 	: $file_type = "gif"; break;
		case "image/x-png" 	: $file_type = "png"; break;
		case "image/png" 	: $file_type = "png"; break;
		default : $file_type = "unk"; break;
	}
	
	$file_full_name = "tmp_".time().".".$file_name;
	if ($file_size > 0 && $file_size <= $max_file_size)
		if ($file_type != "unk") {
				// create associated object
				$picture = new Picture();
				$picture->setname($file_full_name);
				$picture->setup_dir($up_dir_to_insert);
				$picture->setdisplay_name($display_name);
				$picture->insert();
				if ( move_uploaded_file($http_post_files[$input_name]['tmp_name'],$up_dir.$file_full_name)) {
					if ($prefixes)
					{
						foreach ($prefixes as $prefix)
						{
							if ($file_type == 'png') {
								img_png_resize($up_dir.$file_full_name,$up_dir.$prefix.$file_full_name,"w=".$dimensions[$prefix]['w']."&h=".$dimensions[$prefix]['h'].'&zc=1');
							} else {
								img_resize($up_dir.$file_full_name,$up_dir.$prefix.$file_full_name,"w=".$dimensions[$prefix]['w']."&h=".$dimensions[$prefix]['h'].'&zc=1');				
							}
						}
					}
					return $picture->getid();
				}
		}

	return null;
}

function get_picture($name, $tour_id) {
	global $db;
	$query = "SELECT pictures FROM tour WHERE id = ".$tour_id.";";
	$r = $db->query($query);
	$row = $db->fetch($r);
	$pictures = json_decode($row['pictures'], true);
	return $pictures[$name];
}


function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
	
    $path = explode('/', $_SERVER['REQUEST_URI']);
	$module = $path[1];
	$action = $path[2];
    // This will build our "base URL" ... Also accounts for HTTPS :)
    $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';

    // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
    $breadcrumbs = Array("<a href=\"$base\">$home</a>");
	$breadcrumbs[] = "<a href=\"$base$module\">$module</a>";
	preg_match('#act=([^&]*)#', $action, $match);
	$action = str_replace(array('_'), array(' '), $match[1]);
	$breadcrumbs[] = $action;

    // Build our temporary array (pieces of bread) into one big string :)
    return implode($separator, $breadcrumbs);
}

?>